#include "DevParamField.h"

#include "ComStruct.h"

CDevParamField::CDevParamField()
{

}

CDevParamField::CDevParamField(const QString& strKey, const QString& strShowName, int iValueType, const QString& strDefaultName /*= ""*/, const QString& strDefaultValue /*= ""*/)
    : m_strKey(strKey)
    , m_strShowName(strShowName)
    , m_strDefaultName(strDefaultName)
    , m_iValueType(iValueType)
    , m_strDefaultValue(strDefaultValue)
{

}

CDevParamField::~CDevParamField()
{
    Clear();
}

CDevParamField::CDevParamField(const CDevParamField& that)
{
    if (this == &that)
    {
        return;
    }

    CopyFrom(&that);
}

CDevParamField& CDevParamField::operator=(const CDevParamField& that)
{
    if (this != &that)
    {
        CopyFrom(&that);
    }

    return *this;
}

void CDevParamField::CopyFrom(const CDevParamField* pthat)
{
    if (0 == pthat)
    {
        return;
    }

    m_strKey = pthat->m_strKey;
    m_strShowName = pthat->m_strShowName;
    m_iUpdateType = pthat->m_iUpdateType;
    m_strDefaultName = pthat->m_strDefaultName;
    m_iValueType = pthat->m_iValueType;

    m_iValueIndex = pthat->m_iValueIndex;
    m_strDefaultValue = pthat->m_strDefaultValue;
    m_strUnit = pthat->m_strUnit;
    m_iDataType = pthat->m_iDataType;
    m_mapValue2Name = pthat->m_mapValue2Name;
}

void CDevParamField::Clear()
{
    m_strKey = "";
    m_strShowName = "";
    m_iUpdateType = 0;
    m_strDefaultName = "";
    m_iValueType = 0;

    m_iValueIndex = 0;
    m_strDefaultValue = "";
    m_strUnit = "";
    m_iDataType = 0;
    m_mapValue2Name.clear();
}

bool CDevParamField::operator==(const CDevParamField& that) {
    // 防止自身比较
    if (this == &that) return true;

    //任何一个变量值不等，都返回false
    if (this->m_strKey != that.m_strKey) return false;
    if (this->m_strShowName != that.m_strShowName) return false;
    if (this->m_iUpdateType != that.m_iUpdateType) return false;
    if (this->m_strDefaultName != that.m_strDefaultName) return false;
    if (this->m_iValueType != that.m_iValueType) return false;

    if (this->m_iValueIndex != that.m_iValueIndex) return false;
    if (this->m_strDefaultValue != that.m_strDefaultValue) return false;
    if (this->m_strUnit != that.m_strUnit) return false;
    if (this->m_iDataType != that.m_iDataType) return false;
    if (this->m_mapValue2Name != that.m_mapValue2Name) return false;

    // 以上变量值都相等，返回true
    return true;
}

bool CDevParamField::operator!=(const CDevParamField& that) {
    return (!(*this == that));
}

void CDevParamField::InitInt(const QString& strKey, const QString& strShowName, const QString& strDefaultName /*= ""*/, const QString& strDefaultValue /*= ""*/)
{
    m_strKey = strKey;
    m_strShowName = strShowName;
    m_strDefaultName = strDefaultName;
    m_strDefaultValue = strDefaultValue;
    m_iValueType = 0;
}

void CDevParamField::InitFloat(const QString& strKey, const QString& strShowName, const QString& strDefaultName /*= ""*/, const QString& strDefaultValue /*= ""*/)
{
    m_strKey = strKey;
    m_strShowName = strShowName;
    m_strDefaultName = strDefaultName;
    m_strDefaultValue = strDefaultValue;
    m_iValueType = 1;
}

QString CDevParamField::GetKey()
{
    return m_strKey;
}

void CDevParamField::SetKey(const QString& strKey)
{
    m_strKey = strKey;
}

QString CDevParamField::GetShowName()
{
    return m_strShowName;
}

QString CDevParamField::GetShowNameWithUnit()
{
    if (m_strUnit.isEmpty())
    {
        return m_strShowName;
    }
    else
    {
        return QString("%1(%2)").arg(m_strShowName).arg(m_strUnit);
    }
}

void CDevParamField::SetShowName(const QString& strShowName)
{
    m_strShowName = strShowName;
}

QString CDevParamField::GetDefaultName()
{
    return m_strDefaultName;
}

int CDevParamField::GetValueType()
{
    return m_iValueType;
}

void CDevParamField::SetValueTypeToInt()
{
    m_iValueType = 0;
}

void CDevParamField::SetValueTypeToFloat()
{
    m_iValueType = 1;
}

bool CDevParamField::IsTypeOfInt()
{
    return (0 == m_iValueType);
}

bool CDevParamField::IsTypeOfFloat()
{
    return (1 == m_iValueType);
}

int CDevParamField::GetValueIndex()
{
    return m_iValueIndex;
}

void CDevParamField::SetValueIndex(int iValueIndex)
{
    m_iValueIndex = iValueIndex;
}

QString CDevParamField::GetDefaultValue()
{
    return m_strDefaultValue;
}

QString CDevParamField::GetUnit()
{
    return m_strUnit;
}

void CDevParamField::SetUnit(const QString& strUnit)
{
    m_strUnit = strUnit;
}

int CDevParamField::GetDataType()
{
    return m_iDataType;
}

void CDevParamField::SetDataType(int iDataType)
{
    m_iDataType = iDataType;
}

int CDevParamField::GetValueCount()
{
    return m_mapValue2Name.count();
}

bool CDevParamField::IsExist(int ivalue)
{
    return m_mapValue2Name.contains(ivalue);
}

QMap<int, QString> CDevParamField::GetMapValues()
{
    return m_mapValue2Name;
}

QList<QPair<int, QString> > CDevParamField::GetListValues()
{
    QList<QPair<int, QString> > listValue2Name;

    QMapIterator<int, QString> i(m_mapValue2Name);
    while (i.hasNext())
    {
        i.next();

        QPair<int, QString> value2name;
        value2name.first = i.key(); //变量的值
        value2name.second = i.value(); //变量的值的名称

        listValue2Name.append(value2name);
    }

    return listValue2Name;
}

void CDevParamField::ParseValueList(const QString& strLine)
{
    m_mapValue2Name.clear();

    //State0_ValueList=<0:关机><1:送风><2:制冷><3:加热><4:除湿>
    if (strLine.isEmpty())
    {
        return;
    }

    QStringList listPair = strLine.split("><", QString::SkipEmptyParts);
    if (listPair.isEmpty())
    {
        return;
    }

    QString strPair = "";
    for (int i = 0; i < listPair.count(); ++i)
    {
        strPair = listPair.at(i);
        strPair = strPair.replace("<", "");
        strPair = strPair.replace(">", "");
        int iindex = strPair.indexOf(":");
        int ivalue = strPair.left(iindex).toInt();
        QString strName = strPair.mid(iindex+1);

        if (ivalue >= -999999 && ivalue <= 999999 && !strName.isEmpty())
        {
            m_mapValue2Name.insert(ivalue, strName);
        }
    }
}

void CDevParamField::AddInt(int ivalue, const QString& strName)
{
    m_mapValue2Name[ivalue] = strName;
}

void CDevParamField::DeleteInt(int ivalue)
{
    m_mapValue2Name.remove(ivalue);
}

QString CDevParamField::GetIntText(int ivalue)
{
    if (m_mapValue2Name.contains(ivalue))
    {
        return m_mapValue2Name.value(ivalue);
    }

    return "";
}

bool CDevParamField::IsNeedUpdate()
{
    return (1 == m_iUpdateType);
}

int CDevParamField::GetUpdateType()
{
    return m_iUpdateType;
}

void CDevParamField::SetUpdateType(int ivalue)
{
    m_iUpdateType = ivalue;
}
